<html>
<head>
	<title>Survey</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
	<meta name="format-detection" content="telephone=no" />
	
	<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
$comp_id = $_REQUEST['comp_id'];

include 'config.php';

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("mweb_survey");

$sql = 
"select c.CompID, c.CompName, c.Type, c.StreetAddr, c.City, c.Phone, a.datetime, q.Question, a.QuesID, a.QuesType, a.Answer
from company c, ques_grp qg, questions q, answers a
where c.CompID = a.CompID AND qg.QuesGrpID = q.QuesGrpID AND q.QuesID = a.QuesID AND c.CompID = '$comp_id'
order by c.CompID, q.quesID";

$result = mysql_query($sql, $conn) or die(mysql_error());

$row = mysql_fetch_assoc($result);
$comp_name = $row['CompName'];
$comp_type = $row['Type'];
// $comp_sector = $row['Sector'];
// $comp_sub_sector = $row['Subsector'];
// $comp_addr = $row['StreetAddr'] . ", " . $row['City'] . ", " . $row['State'] . ", " . $row['Zip'];
$comp_addr = $row['StreetAddr'] . ", " . $row['City'];
$comp_phone = $row['Phone'];
$comp_dt = $row['datetime'];
$qid = $row['QuesID'];
$q = $row['Question'];
$a = $row['Answer'];
echo "Company Name: $comp_name<br/>";
echo "Company Type: $comp_type<br/>";
// echo "Sector: $comp_sector<br/>";
// echo "Subsector: $comp_sub_sector<br/>";
echo "Address: $comp_addr<br/>";
echo "Phone: <a href='tel:$comp_phone'>$comp_phone</a><br/>";
echo "Date & Time: $comp_dt<br/>";
echo "<input type='button' value='Delete' onclick=\"window.location.href='delete.php?comp_id=$comp_id'\">";
echo "<hr>";
$i = 1;
echo "Question $i: $q <button onclick=\"window.location.href='edit.php?qid=$qid&cid=$comp_id'\">Edit</button><br/>";
echo "Answer: $a";
$i = 2;

$prev = false;

$j = 1;

while ($row = mysql_fetch_assoc($result)) {
	$qid = $row['QuesID'];
	$q = $row['Question'];
	$t = $row['QuesType'];
	$a = $row['Answer'];
	
	if ($prev == false) {
		if ($t == 'Checkbox') {
			$j = 1;
			echo "<br/><br/>Question $i: $q <button onclick=\"window.location.href='edit.php?qid=$qid&cid=$comp_id'\">Edit</button><br/>";
			echo "Answer: <br/>&nbsp;&nbsp;&nbsp;$j. $a";
			$j++;
			$prev = true;
		} else {
			echo "<br/><br/>Question $i: $q <button onclick=\"window.location.href='edit.php?qid=$qid&cid=$comp_id'\">Edit</button><br/>";
			echo "Answer: $a";
			$i++;
		}
	} else {
		if ($t == 'Checkbox') {
			echo "<br/>&nbsp;&nbsp;&nbsp;$j. $a";
			$j++;
		} else {
			$i++;
			echo "<br/><br/>Question $i: $q <button onclick=\"window.location.href='edit.php?qid=$qid&cid=$comp_id'\">Edit</button><br/>";
			echo "Answer: $a";
			$i++;
			$prev = false;
		}
	}
}
?>
<?php include 'footer.php';?>
</body>
</html>